package cn.yj.core.business.controller;

import cn.yj.cgs.dba.entity.vo.ProductFormVo;
import cn.yj.cgs.dba.entity.vo.ProductVo;
import cn.yj.syslog.common.LogType;
import cn.yj.syslog.common.OperateLog;
import cn.yj.config.annotation.Anonymous;
import cn.yj.config.web.AppBaseController;
import cn.yj.core.business.service.iservice.IProductService;
import cn.yj.entity.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 永健
 * @since 2021-06-15 16:14
 */
@RestController
@RequestMapping("/product")
public class ProductController extends AppBaseController<ProductVo> {


    @Resource
    private IProductService productService;


    @Anonymous
    @GetMapping("/pageList")
    public R pageList(@RequestParam Map<String, Object> params) {
        return success(productService.pageList(params, page()));
    }

    @Anonymous
    @GetMapping("/getProductImg/{productNo}")
    public R getProductImg(@PathVariable String productNo) {
        return success(productService.selectProductImg(productNo));
    }

    @GetMapping("/getMyProduct")
    public R getMyProduct(@RequestParam Map<String, Object> params) {
        params.put("userCode", getUserCode());
        return success(productService.getMyProduct(params, page()));
    }

    @GetMapping("/edit/{productNo}")
    public R edit(@PathVariable String productNo) {
        return success(productService.editByNo(productNo));
    }

    @Anonymous
    @GetMapping("/detail/{productNo}")
    public R detail(@PathVariable String productNo) {
        return success(productService.selectByNo(productNo));
    }

    @GetMapping("/myPush")
    public R myPush(@RequestParam Map<String, Object> params) {
        params.put("userCode", getUserCode());
        return success(productService.pageList(params, page()));
    }


    @PostMapping("/save")
    @OperateLog(describe = "发布二手物品",type = LogType.INSERT)
    public R save(@RequestBody ProductFormVo product) {
        product.setUserCode(getUserCode());
        return productService.save(product);
    }

    @PostMapping("/update")
    @OperateLog(describe = "更新二手物品",type = LogType.UPDATE)
    public R update(@RequestBody ProductFormVo product) {
        product.setUserCode(getUserCode());
        return productService.updateById(product);
    }


    /**
     * 将状态修改为已售出
     *
     * @param params - userCode
     *               - productNo
     */
    @PostMapping("/updateStatus")
    @OperateLog(describe = "商品售出",type = LogType.UPDATE)
    public R updateStatus(@RequestBody Map<String, Object> params) {
        params.put("userCode", getUserCode());
        return success(productService.updateStatus(params));
    }

    @PostMapping("/polish")
    @OperateLog(describe = "擦亮物品",type = LogType.UPDATE)
    public R polish(@RequestBody Map<String, Object> params) {
        params.put("userCode", getUserCode());
        return success(productService.polish(params));
    }

    @DeleteMapping("/delete/{productNo}")
    @OperateLog(describe = "删除物品",type = LogType.UPDATE)
    public R delete(@PathVariable String productNo) {
        Map<String, Object> params = getMap("productNo", productNo);
        params.put("userCode", getUserCode());
        return success(productService.deleteByNo(params));
    }


    @Anonymous
    @GetMapping("/productTypes")
    public R productTypes() {
        return success(productService.getTypesList());
    }

    @Anonymous
    @PostMapping("/view/{productNo}")
    public R viewCountAdd(@PathVariable String productNo) {
        productService.viewCountAdd(getUserCode(), productNo);
        return success();
    }


    @DeleteMapping("/delete/img/{ids}")
    @OperateLog(describe = "删除物品",type = LogType.DELETE)
    public R deleteImg(@PathVariable Integer[] ids) {
        return success(productService.deleteProductImg(ids));
    }
}
